﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showHidePanel();            
        }
       
    }
    public void showHidePanel()
    {
        if (Session["UserName"] != null)
        {            
            //lblUserName.Text = Session["UserName"].ToString();
            pnDaDangNhap.Visible = true;
            pnlChuaDangNhap.Visible = false;
        }
        else
        {
            pnDaDangNhap.Visible = false;
            pnlChuaDangNhap.Visible = true;
        }
    }
    protected void ibtnLogout_Click(object sender, ImageClickEventArgs e)
    {
        if (Request.Cookies["UserName"] != null)
        {
            HttpCookie cookie_username = new HttpCookie("UserName", "");
            cookie_username.Expires = DateTime.Now;
            Response.Cookies.Add(cookie_username);
        }

        Session.Remove("UserName");
        pnlChuaDangNhap.Visible = true;
        //lblUserName.Text = "";
        pnDaDangNhap.Visible = false;
        //  Application["DangOnline"] = int.Parse(Application["DangOnline"].ToString()) - 1;
    }
    protected void btnDangNhap_Click(object sender, ImageClickEventArgs e)
    {
            string strUserName = txtUserName.Text;
            string strPassword = txtPassword.Text;
            if ((strUserName != "") && (strPassword != ""))
            {
               

                try
                {
                    ChoDienTuDataContext cdt = new ChoDienTuDataContext();
                    int t = cdt.Login_Customer(strUserName, strPassword).Count();
                   
                    if (t > 0)
                    {
                        Session["UserName"] = txtUserName.Text;                       
                        showHidePanel();
                    }
                }
                catch (Exception ex)
                {
                    //lblUserName.Text = ex.ToString();
                }
            }
            else
            {
                //lblUserName.Text = "damn";
            }
    }
}